package by.epam.trenings.vkphotorest.adapters;

import by.epam.trenings.vkphotorest.R;
import by.epam.trenings.vkphotorest.content.contract.TaskContract;
import by.epam.trenings.vkphotorest.imageLoader.ImageLoaderCallback;
import by.epam.trenings.vkphotorest.utils.TaskApplication;
import by.epam.trenings.vkphotorest.utils.context.ContextHolder;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AlbumAdapter extends CursorAdapter {

	public AlbumAdapter(Context context, Cursor c) {
		super(context, c, true);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {

		ViewHolder viewHolder = (ViewHolder) view.getTag();

		viewHolder.getTextViewTitle().setText(
				cursor.getString(cursor
						.getColumnIndex(TaskContract.AlbumsColumns.TITLE)));
		viewHolder.getTextViewTitle().setSelected(true);

		viewHolder.getTextViewNumber().setText(
				cursor.getString(cursor
						.getColumnIndex(TaskContract.AlbumsColumns.SIZE)));
		TaskApplication.getImageLoader().bind(
				cursor.getString(cursor
						.getColumnIndex(TaskContract.AlbumsColumns.THUMB_SRC)),
				viewHolder.getImageView(), new ImageLoaderCallback() {

					@Override
					public void onSucsess() {

					}

					@Override
					public void onError(Exception e) {
						String warning = ContextHolder
								.getInstance()
								.getContext()
								.getResources()
								.getString(
										R.string.adapter_album_strings_no_internet);

						Toast.makeText(
								ContextHolder.getInstance().getContext(),
								warning + " : " + e.getMessage(),
								Toast.LENGTH_SHORT).show();

					}
				});
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = View.inflate(context, R.layout.view_list_albums, null);
		ViewHolder viewHolder = new ViewHolder();

		viewHolder.setImageView((ImageView) view
				.findViewById(R.id.view_list_albums_image));

		viewHolder.setTextViewNumber((TextView) view
				.findViewById(R.id.view_list_albums_number));

		viewHolder.setTextViewTitle((TextView) view
				.findViewById(R.id.view_list_albums_title));

		view.setTag(viewHolder);
		return view;
	}

	private class ViewHolder {

		/** Preview album */
		private ImageView imageView;

		/** Album title */
		private TextView title;

		/** Album number */
		private TextView number;

		public ImageView getImageView() {
			return imageView;
		}

		public void setImageView(ImageView imageView) {
			this.imageView = imageView;
		}

		public void setTextViewTitle(TextView title) {
			this.title = title;
		}

		public void setTextViewNumber(TextView number) {
			this.number = number;
		}

		public TextView getTextViewTitle() {
			return title;
		}

		public TextView getTextViewNumber() {
			return number;
		}
	}
}
